package cn.edu.bzu.group12.orangenews.service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Random;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.edu.bzu.group12.orangenews.bean.News;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import java.util.List;
import java.util.Map;

/**
 * Servlet implementation class DataLoad
 */

public class DataLoad extends HttpServlet {
	private static final long serialVersionUID = 1L;

/*	static class Person {
		private long id;
		private String firstName;
		private String lastName;

		public long getId() {
			return id;
		}

		public void setId(long id) {
			this.id = id;
		}

		public String getFirstName() {
			return firstName;
		}

		public void setFirstName(String firstName) {
			this.firstName = firstName;
		}

		public String getLastName() {
			return lastName;
		}

		public void setLastName(String lastName) {
			this.lastName = lastName;
		}

		public boolean match(String pattern) {
			return firstName.contains(pattern) || lastName.contains(pattern) || Long.toString(id).contains(pattern);
		}

	}*/

	private static Random r = new Random();
	private static List<News> news = new ArrayList<News>();


	private List<News>result;
	public List<News>getResult(){
		return result;
	}
	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public DataLoad() {
		super();
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		int start=0;
		int length=10;
		String pattern="";
		String draw="1";
		Map<String,String[]>params=request.getParameterMap();
		for(String attr:params.keySet()){
			String[] val=params.get(attr);
			System.out.println(attr+"="+Arrays.toString(val));
			if(attr.equals("start"))
				start=Integer.parseInt(val[0]);
			if(attr.equals("length"))
				length=Integer.parseInt(val[0]);
			if(attr.equals("search[value]"))
				pattern=val[0];
			if(attr.equals("draw"))
				draw=val[0];
		}
		int total=filter(start, length, pattern);
		JSONObject obj = new JSONObject();
		obj.put("draw", draw);
		obj.put("recordsTotal", news.size());
		obj.put("recordsFiltered", total);
		System.out.println(obj.toJSONString());
		obj.put("data", result);
		response.getWriter().println(obj.toJSONString());
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}
	
	private int filter(int start,int length,String pattern){
		result=new LinkedList<News>();
		int total=0;
		for(News s:news){

			if(start-->0)
				continue;
			if(length--<=0)
				continue;
			result.add(s);
		}
		return total;
	}

	public static void main(String[] rags) {
		System.out.println(JSON.toJSONString(news));
		DataLoad load=new DataLoad();
		load.filter(0, 10, "");
		System.out.println(JSON.toJSONString(load.getResult()));
		load.filter(0, 10, "a");
		System.out.println(JSON.toJSONString(load.getResult()));
		load.filter(10, 10, "a");
		System.out.println(JSON.toJSONString(load.getResult()));
		load.filter(20, 10, "a");
		System.out.println(JSON.toJSONString(load.getResult()));
	}

}

